﻿
namespace EasyNet.Solr.Test
{
    using System.Collections.Generic;

    using Commons;

    class ProductSerializer : IObjectSerializer<Product>
    {
        #region IObjectSerializer<Product> Members

        public IEnumerable<SolrInputDocument> Serialize(IEnumerable<Product> objs)
        {
            if (objs == null)
            {
                yield return null;
            }

            foreach (var product in objs)
            {
                var doc = new SolrInputDocument();

                doc.Add("id", new SolrInputField("id", product.Id));

                if (!string.IsNullOrEmpty(product.SKU))
                {
                    doc.Add("sku", new SolrInputField("sku", product.SKU));
                }
                if (!string.IsNullOrEmpty(product.Name))
                {
                    doc.Add("name", new SolrInputField("name", product.Name));
                }
                if (!string.IsNullOrEmpty(product.AlphaNameSort))
                {
                    doc.Add("alphaNameSort", new SolrInputField("alphaNameSort", product.AlphaNameSort));
                }
                if (!string.IsNullOrEmpty(product.Manu))
                {
                    doc.Add("manu", new SolrInputField("manu", product.Manu));
                }
                if (!string.IsNullOrEmpty(product.Cat))
                {
                    doc.Add("cat", new SolrInputField("cat", product.Cat));
                }
                if (product.Features != null && product.Features.Length > 0)
                {
                    doc.Add("features", new SolrInputField("features", product.Features));
                }
                if (!string.IsNullOrEmpty(product.Includes))
                {
                    doc.Add("includes", new SolrInputField("includes", product.Includes));
                }

                doc.Add("weight", new SolrInputField("weight", product.Weight));
                doc.Add("price", new SolrInputField("price", product.Price));
                doc.Add("popularity", new SolrInputField("popularity", product.Popularity));
                doc.Add("inStock", new SolrInputField("inStock", product.InStock));

                if (!string.IsNullOrEmpty(product.Store))
                {
                    doc.Add("store", new SolrInputField("store", product.Store));
                }
                if (product.Title != null && product.Title.Length > 0)
                {
                    doc.Add("title", new SolrInputField("title", product.Title));
                }
                if (!string.IsNullOrEmpty(product.Subject))
                {
                    doc.Add("subject", new SolrInputField("subject", product.Subject));
                }
                if (!string.IsNullOrEmpty(product.Description))
                {
                    doc.Add("description", new SolrInputField("description", product.Description));
                }
                if (!string.IsNullOrEmpty(product.Comments))
                {
                    doc.Add("comments", new SolrInputField("comments", product.Comments));
                }
                if (!string.IsNullOrEmpty(product.Author))
                {
                    doc.Add("author", new SolrInputField("author", product.Author));
                }
                if (!string.IsNullOrEmpty(product.Keywords))
                {
                    doc.Add("keywords", new SolrInputField("keywords", product.Keywords));
                }
                if (!string.IsNullOrEmpty(product.Category))
                {
                    doc.Add("category", new SolrInputField("category", product.Category));
                }
                if (product.ContentType != null && product.ContentType.Length > 0)
                {
                    doc.Add("content_type", new SolrInputField("content_type", product.ContentType));
                }

                doc.Add("last_modified", new SolrInputField("last_modified", product.LastModified));

                if (product.Links != null && product.Links.Length > 0)
                {
                    doc.Add("links", new SolrInputField("links", product.Links));
                }

                yield return doc;
            }
        }

        #endregion
    }
}
